package com.class5.springboot.service.real;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.TbXtUser;
import com.class5.springboot.entity.vo.TbXtUserVo;
import com.class5.springboot.frame.context.SuperServiceImpl;
import com.class5.springboot.frame.repo.Sql;
import com.class5.springboot.frame.repo.dto.PageData;
import com.class5.springboot.frame.repo.dto.ParamMap;
import com.class5.springboot.frame.repo.dto.RecordConvert;
import com.class5.springboot.service.facade.IUserService;
import com.class5.springboot.util.BeanCopyUtils;
import com.class5.springboot.util.DateUtils;
import com.class5.springboot.util.Md5Util;
import com.class5.springboot.util.StringUtils;
import com.class5.springboot.util.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

@Service
public class UserService extends SuperServiceImpl implements IUserService {

    @Override
    @Transactional(readOnly = true)
    public PageData findUserForPage(ParamMap paramMap) {
        Sql sql = Sql.get("select a.id, " +
                "a.user_name as  userName, " +
                "a.real_name as realName, " +
                "a.phone, " +
                "a.org_id, " +
                "b.name as orgName, " +
                "a.role_id, " +
                "c.role_name as roleName, " +
                "a.status, " +
                "a.email, " +
                "a.sex, " +
                "a.remark, " +
                "a.create_time as createTime " +
                "from tb_xt_user a " +
                "left join org_info b " +
                "on a.org_id = b.id " +
                "left join tb_xt_role c " +
                "on a.role_id = c.id").where()
//        Sql sql = Sql.getSelect(TbXtUser.class).where().add("1=1")
                .like("a.user_name",paramMap.getString("userName"))
                .like("a.real_name",paramMap.getString("realName")).parent();
                if(!StringUtils.isNull(paramMap.getString("date_s"))){
                    sql.and().gte("a.create_time",paramMap.getString("date_s")+" 00:00:00").parent();
                }
                if(!StringUtils.isNull(paramMap.getString("date_e"))){
                    sql.and().lte("a.create_time",paramMap.getString("date_e")+" 59:59:59").parent();
                }
        PageData pageData = repo.queryForPage(sql,Map.class,paramMap.getInt("page"),paramMap.getInt("size"));
        pageData.convert(new RecordConvert() {
            @Override
            public Object convert(Object rec) {
                Map map = (Map) rec;
                BeanCopyUtils.handleNullForMap(map);
//                TbXtUserVo vo = new TbXtUserVo();
//                BeanCopyUtils.copyNoEqClass(user,vo);
//                BeanCopyUtils.handleNull(vo,vo);
////                String time = user.getCreateTime().toLocalDateTime()+"";
////                vo.setCreateTime(time.replace("T"," "));//转化为 “yyyy-mm-dd hh:mm:ss”
////                vo.setCreateTime(time.substring(0,time.indexOf("T")));//转化为 “yyyy-mm-dd”
//                vo.setCreateTime(DateUtils.TimestampToDateString(user.getCreateTime(),"1"));
                map.put("createTime",DateUtils.TimestampToDateString((Timestamp) map.get("createTime"),"1"));
                map.put("id",map.get("id")+"");
                return map;
            }
        });
        return pageData;
    }

    @Override
    @Transactional(readOnly = true)
    public TbXtUserVo findUserById(String id) {
        TbXtUser user = repo.get(Long.valueOf(id),TbXtUser.class);
        TbXtUserVo vo = new TbXtUserVo();
        BeanCopyUtils.copyNoEqClass(user,vo);
        BeanCopyUtils.handleNull(vo,vo);
        return vo;
    }

    @Override
    public void updateUserById(JSONObject param) {
        Long id = param.getLong("id");
        TbXtUser tbXtUser = repo.get(id,TbXtUser.class);
        if(tbXtUser==null){
            throw new ServiceException("找不到该用户");
        }
        tbXtUser.setEmail(param.getString("email"));
        tbXtUser.setPhone(param.getString("phone"));
        tbXtUser.setUserName(param.getString("userName"));
        tbXtUser.setSex(param.getString("sex"));
        tbXtUser.setRealName(param.getString("realName"));
        if(!StringUtils.isNull(param.getString("passWord"))){
            tbXtUser.setPassword(Md5Util.md5Hex(param.getString("userName")+param.getString("passWord")));
        }
        repo.update(tbXtUser);
    }

    @Override
    public void changeUserStatus(JSONObject param) {
        Long id = param.getLong("id");
        TbXtUser tbXtUser = repo.get(id,TbXtUser.class);
        if(tbXtUser==null){
            throw new ServiceException("找不到该用户");
        }
        tbXtUser.setStatus(param.getString("status"));
        repo.update(tbXtUser);
    }

    @Override
    public void deleteUserById(String id) {
        TbXtUser tbXtUser = repo.get(Long.valueOf(id),TbXtUser.class);
        if(tbXtUser==null){
            throw new ServiceException("找不到该用户");
        }
        repo.remove(tbXtUser);
    }

    @Override
    public void deleteUserByIds(String idsStr) {
        idsStr = idsStr.trim();
        String param[] = idsStr.split(",");
        if(param.length <= 0){
            throw new ServiceException("参数不能为空!");
        }
        for(String id:param){
            TbXtUser tbXtUser = repo.get(Long.valueOf(id),TbXtUser.class);
            if(tbXtUser==null){
                throw new ServiceException("找不到该用户");
            }
            repo.remove(tbXtUser);
        }
    }
}
